﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ComponentModel;
using System.Windows.Markup;

namespace Vision.Core.ActivityComponents.IO.File
{
	[ContentProperty("Body")]
	[Designer(typeof(DirectoryCreateDesigner))]
	public sealed class DirectoryCreate : NativeActivity
	{
		/// <summary>
		/// 
		/// </summary>
		[Category("Input")]
		public InArgument<string> Path { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Category("Output")]
		public OutArgument<System.IO.DirectoryInfo> Directory { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue(null)]
		public ActivityAction<System.IO.DirectoryInfo> Body { get; set; }

		public DirectoryCreate()
        {
			this.Body = new ActivityAction<System.IO.DirectoryInfo>()
            {
				Argument = new DelegateInArgument<System.IO.DirectoryInfo>()
                {
                    Name = "directoryInfo"
                }
            };
        }

		// If your activity returns a value, derive from NativeActivity<TResult>
		// and return the value from the Execute method.
		protected override void Execute(NativeActivityContext context)
		{
			System.IO.DirectoryInfo di = null;

			if (System.IO.Directory.Exists(this.Path.Get(context)))
				di = new System.IO.DirectoryInfo(this.Path.Get(context));
			else
				di = System.IO.Directory.CreateDirectory(this.Path.Get(context));
			
			this.Directory.Set(context, di);

			// schedule the next action
			if (this.Body != null && this.Body.Handler != null && di != null)
			{
				context.ScheduleAction(this.Body, di);
			}
		}
	}
}
